package com.zhitan.ems.app.api.patrol;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhitan.ems.app.patrol.entity.PatrolEntity;
import com.zhitan.ems.app.patrol.entity.PatrolNoticeEntity;
import com.zhitan.ems.app.patrol.service.PatrolNoticeService;
import com.zhitan.ems.app.patrol.service.PatrolService;
import com.zhitan.ems.app.patrol.vo.PatrolNoticeVO;
import com.zhitan.ems.app.patrol.wrapper.PatrolNoticeWrapper;
import com.zhitan.ems.common.core.domain.R;
import com.zhitan.ems.common.utils.StringUtils;
import com.zhitan.ems.framework.core.vo.VPage;
import com.zhitan.ems.framework.core.vo.VQueryKit;
import com.zhitan.ems.mini.api.ApiBaseAction;
import com.zhitan.ems.mini.api.ApiHeaderVo;
import com.zhitan.ems.mini.api.ApiThreadContext;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
 * 上报消息
 * @author zhitan
 * 
 * @date 2024-11-02 14:05:24
 */
@Api(tags = "App管理--巡检项目--巡检上报", value = "巡检上报")
@RestController
@RequestMapping("api/patrol/patrolnotice")
public class ApiPatrolNoticeController extends ApiBaseAction{
    @Autowired
    private PatrolNoticeService patrolNoticeService;
    @Autowired
    private PatrolService patrolService;
    /**
     * 列表
     */
    @ApiOperation(value = "列表", response = VPage.class)
    @ApiImplicitParams({
		        @ApiImplicitParam(name = "name", value = "标题",  paramType = "query", required = false, dataType =  "String"),
		})
    @GetMapping("/browse")
    public R<VPage<PatrolNoticeVO>> list(@RequestParam Map<String, Object> params){
        QueryWrapper<PatrolNoticeEntity> queryWrapper=VQueryKit.getQueryWrapperByMap(params,Arrays.asList("name"), PatrolNoticeEntity.class);
        queryWrapper.orderByDesc("id");
         queryWrapper.like(StringUtils.isNotNull(params.get("name")),"name", params.get("name"));
        IPage<PatrolNoticeEntity> pages = patrolNoticeService.page(VQueryKit.getPage(params), queryWrapper);
        return R.data(PatrolNoticeWrapper.build().toVPageVO(pages));
    }
    /**
     * 信息
     */
    @ApiOperation(value = "信息", response = PatrolNoticeVO.class)
    @GetMapping("/detail/{id}")
    public R<PatrolNoticeVO> detail(@PathVariable("id") Long id){
		PatrolNoticeEntity patrolNotice = patrolNoticeService.getById(id);
	    return R.data(PatrolNoticeWrapper.build().entityVO(patrolNotice));
    }
     /**
     * 保存&更新
     */
    @ApiOperation(value = "保存&更新", response = Boolean.class)
    @PostMapping("/submit")
    public R<PatrolNoticeVO> submit(@Valid @RequestBody PatrolNoticeVO patrolNotice){
    	ApiHeaderVo apiHeaderVo=ApiThreadContext.getApiHeader();  
   		if(patrolNotice.getId()==null) {
            patrolNotice.setCreateAt(new Date());
            patrolNotice.setIsDelete(0);
        } 
   		patrolNotice.setUpdateAt(new Date());
   		patrolNotice.setUserId(apiHeaderVo.getUserId());
   		patrolNotice.setUserName(apiHeaderVo.getUserName());
    	if(patrolNoticeService.saveOrUpdate(patrolNotice)) {
    		if(patrolNotice.getPatrolId()!=null&&patrolNotice.getPatrolId()>0) {
    			PatrolEntity patrol = patrolService.getById(patrolNotice.getPatrolId());
    			if(patrol!=null) {
    				patrol.setNoticeTotalNum(patrol.getNoticeTotalNum()+1);
    				patrol.setUpdateAt(new Date());
    				patrolService.updateById(patrol);
    			}
    		}
    		return R.ok(patrolNotice);
    	 }
    	return R.error("数据错误");
    }

}
